package com.jackrain.nea.conf;

import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.config.PropertiesConf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.resource.GzipResourceResolver;

import java.util.concurrent.TimeUnit;

/**
 * Created by Lucky dog on 2016/10/20.
 */
@Configuration
public class StaticResourceConfig extends WebMvcConfigurerAdapter {

    @Autowired
    PropertiesConf pconf;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        int maxAge = TypeUtils.castToInt(pconf.getProperty("raincloud.cachecontrol.maxage"));


        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/")
                .setCacheControl(CacheControl
                        .maxAge(maxAge, TimeUnit.HOURS)
                        .cachePublic())
                .resourceChain(false)
                .addResolver(new GzipResourceResolver());


        super.addResourceHandlers(registry);
    }
}